$(document).ready(function () {




    /**********************定位******************/
    /*
    * 优先使用登录用户自带的定位region.code和region.name
    * 未登录时，优先使用用户选中的定位--sessionStorage.getItem('userChooseLocationCode');
    * 最后使用自动定位sessionStorage.getItem('autolocationcode');*/


        if(islogin()){

            sessionStorage.setItem('userChooseLocationCode',getUserInfo().region.code);       //更新来自登录用户的code
            $(".locationCity ").html(getUserInfo().region.name);

        }



    //轮播图
    // var swiper = new Swiper('.swiper-container', {
    //     pagination: '.swiper-pagination',
    //     nextButton: '.swiper-button-next',
    //     prevButton: '.swiper-button-prev',
    //     slidesPerView: 1,
    //     paginationClickable: true,
    //     spaceBetween: 30,
    //     loop: true,
    //     autoplay: 4000
    // });


    /****************************banner滚动********************************/

    myAjaxFunc({
        url: geturl + "getBanner",
        type: 'get',
        dataType: 'json',
        mySuccess: function (data) {

            var bannerContent = data.data.bannerList,
                bannerList = '';
            for (var i = 0, blg = bannerContent.length; i < blg; i++) {
                bannerList += '<div data-id="'+ bannerContent[i].productId + '" class="swiper-slide"><img  data-id="' + bannerContent[i].url + '" src="' + geturlimg + bannerContent[i].pictureUrl + '"></div>'
            }
            $('#indexBanner').append(bannerList);

            var swiper = new Swiper('.swiper-container', {

                pagination: '.swiper-pagination',
                nextButton: '.swiper-button-next',
                prevButton: '.swiper-button-prev',
                slidesPerView: 1,
                paginationClickable: true,
                spaceBetween: 30,
                loop: true,
                autoplay: 4000

            });

            $(".swiper-slide").click(function () {

                var productId = $(this).data('id'),
                    url = $(this).find('img').data('id');


                if (isNull(url)) {
                    if (isNull(productId)) {
                        var urlTo = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.dongxiangtech.jiedaijia';
                        switchLocation(urlTo)
                    } else {
                        var url = 'loansDetail.html?id=' + productId;
                        switchLocation(url);
                    }
                } else {
                    switchLocation(url)
                }


            })
        }
    });



    /****************************产品推荐*4个tab********************************/

    myAjaxFunc({
        url: geturl + "getRecommend",
        type: 'get',
        dataType: 'json',
        mySuccess: function (data) {

            var fourType = data.data.recommendList,pcType='';
            for (var i = 0, flg = fourType.length; i < flg; i++) {

                pcType+='      <a href="needLoans.html?formfourType=sesstion&dis=' + fourType[i].propertyIdObj.name + '" target="_blank">' +
                    '            <img  src="' + geturlimg + fourType[i].pictureUrl + '">' +
                    '            <div class="fourTypeText">' +
                    '                <h4>' + fourType[i].propertyIdObj.name + '</h4>' +
                    '                <p>最新贷款产品</p>' +
                    '            </div>' +
                    '        </a>'

            }
            $('#fourType').append(pcType)
        }
    });


    /****************************热门产品*********************************/


    let code = sessionStorage.getItem('userChooseLocationCode'),        //手动获取或者登录用户的
        dataCode = sessionStorage.getItem('autolocationcode');     //自动定位

    if (notNull(code)) { code=dataCode;}


    function getdata(dataCode, currentPage) {
        myAjaxFunc({
            url: geturl + "getPageDateHot",
            type: 'post',
            dataType: 'json',
            data: {regionCode: dataCode, listCurrentPage: currentPage,platform: 'h5' + getPlatform()},
            mySuccess: function (data) {

                var content = data.data.pageDate.list
                    , contentpage = data.data.pageDate.extra
                    , loansMap = '';

                for (let i = 0, ig = 5; i < ig; i++) {


                    let marks = content[i].mark,

                        a = content[i].loanRate,        //利率
                        b = content[i].loanRateUnit,    //利率单位
                        c = content[i].loanLimitLow,    //低额度
                        d = content[i].loanLimitHigh,   //高额度
                        e = content[i].loanSpeed,       //放款速度
                        f = content[i].loanSpeedUnit;   //放款单位

                    loansMap+=
                        '                <div class="loansItem">' +
                        '                    <img class="loansLogo" src="'+ geturlimg+content[i].logoUrl+'">' +
                        '                    <div class="loanInfo">' +
                        '                      <div class="loanInfoLeft">' +
                        '                          <h3>'+content[i].name+'</h3>' +
                        '                          <p>利息：<i>'+a+'%/'+b+'</i></p>' +
                        '                          <p>额度：<i>'+c+'-'+d+'元</i></p>' +
                        '                      </div>' +
                        '                        <div class="loanInfoRight">' +
                        '                            <p>最快放款时间：'+e+f+'</p>' +
                        '                            <a href="loansDetail.html?id='+content[i].id+'" target="_blank">查看详情</a>' +
                        '                        </div>' +
                        '                    </div>' +
                        '                </div>'





                }

                $('#indexHotLoansList').empty().append(loansMap);



            }
        });
    }

     getdata(code, 1);


    /****************************首页侧边栏热门帖子*********************************/

    sidebarfn('sidebarPost','.loansDetailHotPost');

    /****************************首页侧边栏最新贷款*********************************/

    sidebarfn('sidebarProduct','.NewLoansProductList');

    /****************************首页热门社区*********************************/


    function getIndexHotGroupList() {

        myAjaxFunc({
            url: community + "getIndexHotGroupList",
            type: 'post',
            dataType: 'json',
            data: {groupId: '1000',listCurrentPage:'1'},
            mySuccess: function (data) {

                let content = data.data.pageDate.list
                    , hotPost = '';


                for (let i = 0, ig = 4; i < ig; i++) {


                    // 现在老版本读取图片，imgUrls的第一张
                    // 新版本后应该读取封面图片，就无需用到此处
                    let imgUrl=content[i].imgUrls;
                    let imgList=[];

                    if (notNull(imgUrl)) {
                        imgList = imgUrl.split(',');
                    }
                    //


                    let marks = content[i].mark,

                        a = content[i].loanRate,        //利率
                        b = content[i].loanRateUnit,    //利率单位
                        c = content[i].loanLimitLow,    //低额度
                        d = content[i].loanLimitHigh,   //高额度
                        e = content[i].loanSpeed,       //放款速度
                        f = content[i].loanSpeedUnit;   //放款单位

                    hotPost+=' <a href="groupItem.html?groupId=1" class="talkItem">' +
                        '            <img src="http://www.jiedaijia.cn/creditWell/resource/dajindai.png">' +
                        '            <div class="talkGroupText">' +
                        '                <h4>贷款新手区</h4>' +
                        '                <div class="memberPost">' +
                        '                    <span class="member">成员：1634</span>' +
                        '                    <span class="post">帖子：1000</span>' +
                        '                </div>' +
                        '                <div class="newPostTitle">' +
                        '                    <span class="newPostTitle">今天六一儿童节，祝福大家节日快乐！虽然你年龄已经过了过六一的时间了，但你的智商可以！</span>\n' +
                        '                    <time >15分钟前</time>' +
                        '                    <span class="userName">天神小宝</span>' +
                        '                </div>' +
                        '            </div>' +
                        '        </a>'

                   }

                $(eml).append(hotPost);



            }
        });
    }


    /****************************首页社区帖子********************************/

     let indexGroupPostList= $("#postInnertext");

    myAjaxFunc({
        url: community + "getPagePost",
        type: 'post',
        dataType: 'json',
        data: {groupId: 1000, listCurrentPage: 1},
        mySuccess: function (data) {
            if (isResponseSuccess('success', data)) {
                let postData = isResponseSuccess('getdata', data);
                postData.list.roles = set_roles();   //配置管理员信息
                postData.list.listCurrentPage = postData.extra.listCurrentPage;  //配置当前页
                postData.list.geturl = geturlimg;     //配置图片加载地址
                let leng = postData.list.length;

                    for (let i = 0; i < leng; i++) {
                        let imgList = '';
                        let imgUrl = postData.list[i].imgUrls;
                        if (notNull(imgUrl)) {
                            imgList = postData.list[i].imgUrls.split(',');
                        }
                        postData.list[i].imgUrls = imgList;
                        postData.list[i].textContentShort = html_encode(postData.list[i].textContentShort);

                        console.log(postData.list[i].imgUrls)

                    }


                //配置图片为数组

                let pageInfo = postData.extra,
                    allpage = pageInfo.listPageCount, //总页数
                    atpage = pageInfo.listCurrentPage; //当前页数

                console.log(postData);

                var html = template('index-post-list-template', postData);

                indexGroupPostList.append(html);

                setTimeout(showPri(), 200);//返回显示点赞数据


                /*******点击图片********/
                $('.Gallery').on('click', 'li', function () {
                    event.stopPropagation();
                    var innde = '<div class="swiper-container" ><div class="swiper-wrapper" ></div><div class="swiper-pagination"></div></div>';
                    var clickIndex = $(this).index(),
                        showBoxImg = '',
                        allImg = $(this).parent().find('img');
                    for (var i = clickIndex, ig = allImg.length, num = 0; num < ig;) {
                        if (i >= ig)
                            i = i - ig;
                        var src = allImg[i].currentSrc;
                        showBoxImg += '<div class="swiper-slide" ><img src="' + src + '"></div>';
                        i++;
                        num++;
                    }
                    $('#imgBox').append(innde);
                    $('.swiper-wrapper').append(showBoxImg);
                    $("body").on("touchmove", function (event) {
                        event.preventDefault;
                    }, false).addClass('over');
                    $('#imgBox').show();
                    var swiper = new Swiper('.swiper-container', {
                        pagination: '.swiper-pagination',
                        paginationType: 'fraction',
                        loop: true
                    });
                    $('#imgBox').click(function () {
                        $('#imgBox').hide().html('');
                        $('body').removeClass('over').off("touchmove");
                        swiper = ''
                    });
                    event.stopPropagation();

                });//查看大图
                changeImg();//更改图片样式
            }
        }
    });









    //优化图片显示
    function changeImg() {

        var _imgb = $('.postImg img');
        var postCoverImg = $('.postCoverImg');
        _imgb.load(function () {
            var Imgheight = $(this).height();
            var ImgWidth = $(this).width();
            if (Imgheight > ImgWidth) {
                $(this).addClass('forWidth')
            } else {
                $(this).addClass('forHeight')
            }

        })

        postCoverImg.load(function () {
            var Imgheight = $(this).height();
            var ImgWidth = $(this).width();
            if (Imgheight > ImgWidth) {
                $(this).addClass('forWidth')
            } else {
                $(this).addClass('forHeight')
            }

        })


    }

    changeImg()



    var groupCommentTop='1634';
    // setTimeout(function () {
    //     groupCommentTop=getDistance( $('.rightNewProduct')).top;
    //     console.log('topboxtx'+groupCommentTop);
    //
    // },1000)

    /*右边侧栏滚动显示*/
    $(document).scroll(function () {
        let scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
            , che = document.documentElement.scrollHeight - (document.documentElement.clientHeight + scrollTop);

        if(scrollTop>420){
            $('.fix_ul').show()
        }else {
            $('.fix_ul').hide()

        }



        console.log(scrollTop);

        var rightboxtx=getDistance( $('.leftGroupPost')).right;
        var bottomboxtx=getDistance( $('.leftGroupPost')).bottom;

        // console.log('topboxtx'+groupCommentTop);
        // console.log('bottomboxtx'+bottomboxtx);


        if(scrollTop>groupCommentTop){
            $('.rightNewProduct').addClass('fixRight');
            $('.rightNewProduct').css({'right':rightboxtx-340+'px'})
        }else {
            $('.rightNewProduct').removeClass('fixRight');

        }
        // console.log(scrollTop);

        if(bottomboxtx<-10){
            $('.rightNewProduct').css({'right':rightboxtx-340+'px'})
            $('.rightNewProduct').show();

        }else {
            $('.rightNewProduct').hide();

        }


    });
    /*END*/




});

